<%@ page import="javax.security.auth.login.LoginContext"%>
<%@ page import="org.ow2.bonita.facade.QueryRuntimeAPI"%>
<%@ page import="org.ow2.bonita.light.LightProcessDefinition"%>
<%@page import="org.ow2.bonita.facade.def.majorElement.ProcessDefinition"%>
<%@ page import="org.ow2.bonita.facade.uuid.ProcessDefinitionUUID"%>
<%@ page import="org.ow2.bonita.util.AccessorUtil"%>
<%@ page import="org.ow2.bonita.util.SimpleCallbackHandler"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Collections"%>
<%@page import="org.ow2.bonita.facade.runtime.ActivityState"%>
<%@page import="org.ow2.bonita.light.LightTaskInstance"%>
<%@page import="java.util.Collection"%>
<%@page import="org.ow2.bonita.facade.uuid.ActivityInstanceUUID"%>
<%@page import="org.ow2.bonita.facade.QueryDefinitionAPI"%>
<%@page import="org.ow2.bonita.facade.def.majorElement.ProcessDefinition.ProcessState"%>
<%@page import="java.util.Set"%>

<%
final Object usernameObject = request.getSession().getAttribute("username");
if (usernameObject == null) {
  response.sendRedirect("index.jsp");
}
final String username = (String) usernameObject;

final Object isAdminObject = request.getSession().getAttribute("isadmin");
final boolean isAdmin = isAdminObject != null && ((Boolean) isAdminObject).booleanValue();
final String msg = request.getParameter("msg");


final int numberOfElementsToRetrive = 20;
Collection<LightTaskInstance> readyTaskList = Collections.emptyList();
Collection<LightTaskInstance> doneTaskList = Collections.emptyList();
Set<LightProcessDefinition> enabledProcesses = Collections.emptySet();
try {
  final LoginContext loginContext = new LoginContext("BonitaStore", new SimpleCallbackHandler(username, ""));
  loginContext.login();
  final QueryRuntimeAPI queryRuntimeAPI = AccessorUtil.getQueryRuntimeAPI();
  final QueryDefinitionAPI queryDefinitionAPI = AccessorUtil.getQueryDefinitionAPI();
  
  readyTaskList = queryRuntimeAPI.getLightTaskList(ActivityState.READY);
  doneTaskList = queryRuntimeAPI.getLightTaskList(ActivityState.FINISHED);
  
  enabledProcesses = queryDefinitionAPI.getLightProcesses(ProcessDefinition.ProcessState.ENABLED);
  
  loginContext.logout();
} catch (Exception e) {
  e.printStackTrace();
  Throwable t = e;
  while (t.getCause() != null) {
    t = t.getCause();
  }
  final String errorMsg = "Error while listing tasks: " + t.getMessage();
  response.sendRedirect("index.jsp?msg=" + errorMsg);  
}
%>

<jsp:include page="header.html"/>
<html>
<head>
<title> Homepage </title>
</head>
<body>
<h1> HOMEPAGE</h1>
<%if (msg != null) {%>
  <b><%=msg %></b><br><br>
<%}%>
<%if (isAdmin) {%>
  <a href="processes.jsp">Manage processes</a>&nbsp;&nbsp;&nbsp;
  <a href="instances.jsp">Manage instances</a>&nbsp;&nbsp;&nbsp;
  <a href="HomeBAM.jsp">Business Activity Monitoring</a>&nbsp;&nbsp;&nbsp;
  <br><br>
<%}%>

<TABLE BORDER="1" CELLPADDING="10" WIDTH="600px">
<CAPTION>LIST OF TASKS TO PERFORM</CAPTION>
<TR>
  <TH>Task Name</TH>
  <TH>Process</TH>
  <TH>Actions</TH>
</TR>

<%
for (LightTaskInstance task : readyTaskList) {
  final String label = task.getDynamicLabel() != null ? task.getDynamicLabel() : task.getActivityLabel();
  final String name = task.getActivityName();
  final String display = label != null ? label : name;
  final ActivityInstanceUUID taskUUID = task.getUUID();
  final ProcessDefinitionUUID processUUID = task.getProcessDefinitionUUID();
%>
<TR>
<TD><%=display %></TD>
<TD><%=processUUID %></TD>
<TD><a href="taskDetails.jsp?taskUUID=<%=taskUUID %>">Details</a></TD>
</TR>
<%}%>
</TABLE>

<BR><BR>

<TABLE BORDER="1" CELLPADDING="10" WIDTH="600px">
<CAPTION>LIST OF DONE TASKS</CAPTION>
<TR>
  <TH>Task Name</TH>
  <TH>Process</TH>
  <TH>Actions</TH>
</TR>

<%
for (LightTaskInstance task : doneTaskList) {
  final String label = task.getDynamicLabel() != null ? task.getDynamicLabel() : task.getActivityLabel();
  final String name = task.getActivityName();
  final String display = label != null ? label : name;
  final ActivityInstanceUUID taskUUID = task.getUUID();
  final ProcessDefinitionUUID processUUID = task.getProcessDefinitionUUID();
%>
<TR>
<TD><%=display %></TD>
<TD><%=processUUID %></TD>
<TD><a href="taskDetails.jsp?taskUUID=<%=taskUUID %>">Details</a></TD>
</TR>
<%}%>
</TABLE>
<BR><BR>

<TABLE BORDER="1" CELLPADDING="10" WIDTH="600px">
<CAPTION>LIST OF PROCESSES</CAPTION>
<TR>
  <TH>Process Name</TH>
  <TH>Version</TH>
  <TH>Actions</TH>
</TR>

<%
for (LightProcessDefinition process : enabledProcesses) {
  final ProcessDefinitionUUID processUUID = process.getUUID();
%>
<TR>
<TD><%=process.getLabel() %></TD>
<TD align="center"><%=process.getVersion() %></TD>
<TD>
  <a href="actions/apiCall.jsp?action=createInstance&back=home&processUUID=<%=processUUID %>">Create a new instance</a>&nbsp;&nbsp;&nbsp;
</TD>
</TR>  
<%}%>
</TABLE>

<br><br>
</body>
</html>
<jsp:include page="footer.html"/>